package me.khrystal.rxnews.module.photo.model;

import com.socks.library.KLog;

import java.util.List;

import me.khrystal.rxnews.bean.SinaPhotoList;
import me.khrystal.rxnews.callback.RequestCallback;
import me.khrystal.rxnews.http.HostType;
import me.khrystal.rxnews.http.manager.RetrofitManager;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/29
 * update time:
 * email: 723526676@qq.com
 */
public class IPhotoListInteractorImpl implements IPhotoListInteractor<List<SinaPhotoList.DataEntity.PhotoListEntity>>{

    @Override
    public Subscription requestPhotoList(final RequestCallback<List<SinaPhotoList.DataEntity.PhotoListEntity>> callback, String id, int startPage) {


        return RetrofitManager.getInstance(HostType.SINA_NEWS_PHOTO)
                .getSinaPhotoListObservable(id,startPage).doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        callback.beforeRequest();
                    }
                }).subscribeOn(AndroidSchedulers.mainThread()).doOnError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        KLog.e("错误时处理:"+ throwable + "----" +throwable
                            .getLocalizedMessage());
                    }
                }).flatMap(new Func1<SinaPhotoList, Observable<SinaPhotoList.DataEntity.PhotoListEntity>>() {
                    @Override
                    public Observable<SinaPhotoList.DataEntity.PhotoListEntity> call(SinaPhotoList sinaPhotoList) {
                        return Observable.from(sinaPhotoList.data.list);
                    }
                }).toSortedList(
                        new Func2<SinaPhotoList.DataEntity.PhotoListEntity, SinaPhotoList.DataEntity.PhotoListEntity, Integer>() {
                            @Override
                            public Integer call(SinaPhotoList.DataEntity.PhotoListEntity photoListEntity, SinaPhotoList.DataEntity.PhotoListEntity photoListEntity2) {
                                return photoListEntity2.pubDate > photoListEntity.pubDate ? 1 : photoListEntity2.pubDate == photoListEntity.pubDate ? 0 :-1;
                            }
                        }
                ).subscribe(new Subscriber<List<SinaPhotoList.DataEntity.PhotoListEntity>>() {
                    @Override
                    public void onCompleted() {
                        callback.requestComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.requestError(e + "\n" + e.getLocalizedMessage());
                    }

                    @Override
                    public void onNext(List<SinaPhotoList.DataEntity.PhotoListEntity> photoListEntities) {
                        callback.requestSuccess(photoListEntities);
                    }
                });
    }
}
